const express = require('express')
const { spawn } = require('child_process')
const { findAvailablePort } = require("./AvailablePort")

// 端口
let port = 3000

const app = express()

// 配置静态目录，使其可被访问
app.use(express.static('./src/'))


function server_start() {
    findAvailablePort(port, availablePort => {
        port = availablePort
        const url = 'http://localhost:' + availablePort
        // 启动服务器
        server = app.listen(availablePort, () => {
            console.log(`Server running at \x1B[32m${url}\x1B[0m`)
        })

        // 执行 CMD 命令
        const cmd = spawn('cmd.exe', ['/c', 'start', ' ', url]);

        cmd.on('close', code => {
            console.log('命令执行完成，退出码：', code);
        })
    })
}

module.exports = { server_start }